import pytest
import yaml
import time
import json
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class Ceshiren:

    def __init__(self):
        self.driver = webdriver.Chrome()

    def open(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.maximize_window()

    def close(self):
        self.driver.quit()

    def cookei_get(self):
        # 1. 访问登录页面
        self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.CLASS_NAME, 'd-button-label').click()
        # time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="login-buttons"]/button[2]').click()
        # time.sleep(2)
        # self.driver.get(
        #     "https://open.weixin.qq.com/connect/qrconnect?appid=wxae95a37f53c9686c&client_id=wxae95a37f53c9686c&redirect_uri=https%3A%2F%2Fceshiren.com%2Fauth%2Foauth2_basic%2Fcallback&response_type=code&scope=snsapi_login&state=3b8b5edbe19434e669f5268b4d955200d5d9f5acb63cf187")
        # # 2. 等待20s，人工扫码操作 或者使用显式等待url发生变化
        # # time.sleep(20)
        WebDriverWait(self.driver, 20).until(
            expected_conditions.element_to_be_clickable(
                (By.ID, 'whos-online')))
        # # 3. 等成功登陆之后，再去获取cookie信息
        cookie = self.driver.get_cookies()
        # 4. 将cookie存入一个可持久存储的地方，文件
        # 打开文件的时候添加写入权限
        with open("cookie.yaml", "w") as f:
            # 第一个参数是要写入的数据
            yaml.safe_dump(cookie, f)

    def add_cookie(self):
        # 1. 访问ceshiren主页面
        self.driver.get("https://ceshiren.com/")
        # 2. 定义cookie，cookie信息从已经写入的cookie文件中获取
        cookie = yaml.safe_load(open("cookie.yaml"))
        # 3. 植入cookie
        for c in cookie:
            self.driver.add_cookie(c)
        # print(123456)
        time.sleep(2)
        # 4.再次访问ceshiren页面，发现无需扫码自动登录，而且可以多次使用
        self.driver.get("https://ceshiren.com")
        self.driver.set_window_size(1936, 1056)

    def search_open(self):
        self.driver.find_element(By.ID, "search-button").click()

    def search_text(self, keyword):
        element = self.driver.find_element(By.ID, 'search-term')
        element.click()
        element.send_keys(keyword)
        time.sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="ember7"]/header/div/div/div[2]/div/div/div/div/div[1]/div/a[2]').click()
        time.sleep(1)
        self.driver.find_element(By.ID, 'search-in-category-header').click()
        time.sleep(1)
        element = self.driver.find_element(By.ID, 'search-in-category-header')
        element.send_keys('霍格沃兹答疑区')
        time.sleep(1)
        element.send_keys(Keys.ENTER)
        # time.sleep(1)
        self.driver.find_element(By.ID, 'search-status-options-header').click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '[title=已被解决]').click()
        time.sleep(1)
        self.driver.find_element(By.ID, 'search-with-tags-header').click()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, '[title="selenium"]').click()
        # time.sleep(1)
        self.driver.find_element(By.ID, 'search-with-tags-header').click()
        time.sleep(2)
        self.driver.find_element(By.CLASS_NAME, "d-button-label").click()
        time.sleep(1)
        text = self.driver.find_elements(By.CLASS_NAME, 'topic-title')
        title = []
        for i in text:
            title.append(i.text)
            # print(title)
            title.append('\n')
            if i == 4:
                break
            print(title)
        with open("title.yaml", "a") as f:
            # 第一个参数是要写入的数据
            yaml.safe_dump(title, f, default_flow_style=False, encoding='GBK', allow_unicode=True)
        return title

